//
//  CinemaViewController.m
//  MINFOBBS
//
//  Created by 蔡龙生 on 13-12-2.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "CinemaViewController.h"
#import "SBJsonParser.h"
#import "DetectionSystemVersion.h"
//#import "ScheduleViewController.h"
#import "MovieShow.h"
#import "UIColor+expanded.h"
@interface CinemaViewController ()

@end

@implementation CinemaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [self.navigationItem setHidesBackButton:YES];
    [self BackButton];
    pageLimit=10;
    pageStart=0;
    self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    loadMore=[[LoadMore alloc] init];
    loadMore.frame=CGRectMake(0, 0, 320, 32);
    [self.table addSubview:loadMore];
    self.table.showsVerticalScrollIndicator=YES;
    self.table.bounces=YES;
    [ self GetWeatherSearch:self.query Location:self.city];
	// Do any additional setup after loading the view.
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(movieShowArray.count>0){
        return movieShowArray.count;
    }
    else{
        return 0;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier=@"CinemaIdentifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        cell.backgroundColor=[UIColor clearColor];
    }
    NSUInteger row=[indexPath row];
    NSString *dd=[[movieShowArray objectAtIndex:row] objectForKey:@"Title"];
    cell.textLabel.text=dd;
    cell.imageView.image=[UIImage imageNamed:@"电影演出"];
    cell.textLabel.backgroundColor=[UIColor clearColor];
    if (indexPath.row%2==1) {
        UIView *vie=[[UIView alloc]init];
        vie.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
        cell.backgroundView=vie;
    }
    else{
        cell.backgroundColor=[UIColor whiteColor];
    }
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    return cell;
}
-(float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 35;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *result=[[UIView alloc]init];
    result.backgroundColor=[UIColor clearColor];
    return result;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    PrevCGPoint=scrollView.contentOffset;
    if (movieShowArray.count<total) {
        if(PrevCGPoint.y>scrollView.contentSize.height-scrollView.frame.size.height){
            if (loadingmore) return;
            loadingmore=YES;
            pageStart=pageStart+pageLimit;
            //            NSLog(@"%d",pageStart);
            [self GetWeatherSearch:self.query Location:self.city];
        }
    }

}

#pragma mark - Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    NSUInteger row=[indexPath row];
//    NSArray *schedule=[[[movieShowArray objectAtIndex:row] objectForKey:@"Properties"] objectForKey:@"Property"];
//    ScheduleViewController *scheduleViewController =[[ScheduleViewController alloc] init];
//    scheduleViewController.showCity=[[schedule objectAtIndex:0] objectForKey:@"text"];
//    scheduleViewController.movieName=[[schedule objectAtIndex:1] objectForKey:@"text"];
//    scheduleViewController.cinema=[[schedule objectAtIndex:4] objectForKey:@"text"];
//    scheduleViewController.showTime=[[schedule objectAtIndex:3] objectForKey:@"text"];
//    [self.navigationController pushViewController:scheduleViewController animated:YES];
    
    MovieShow *movieShow=[[MovieShow alloc]init];
    movieShow.arrReturn=[movieShowArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:movieShow animated:YES];
}

#pragma mark - Get Lottery Ticket Data
-(void)GetWeatherSearch:(NSString*)Query Location:(NSString*)City{
    loadMore.loadingTitle.text=@"正在加载...";
    [loadMore.loadingAni startAnimating];
    tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:Query  Start:pageStart Limit:pageLimit];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"movieCinema"];
}

-(void)requestFinished:(ASIHTTPRequest*)request{
    NSString *response = [request responseString];
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
    
    if ([request.username isEqual:@"movieCinema"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess==YES) {
            NSMutableArray *arrReturn=[[NSMutableArray alloc] init];
            [arrReturn addObject:[[Result objectForKey:@"Response"] objectForKey:@"Categories"]];
            for (int i=0; i<arrReturn.count; i++) {
                NSArray *arr=[[arrReturn objectAtIndex:i] objectForKey:@"Category"];
                for (int j=0; j<arr.count; j++) {
                    if([[[arr objectAtIndex:j] objectForKey:@"Entrance"] isEqual:@"MovieShow.SearchMovieShow"])
                    {
                            if(movieShowArray==nil){
                                movieShowArray=[[NSMutableArray alloc] init];
                                total=[[[[arr objectAtIndex:j]objectForKey:@"Items"] objectForKey:@"total"] integerValue];
                            }
                            [movieShowArray addObjectsFromArray:[[[arr objectAtIndex:j] objectForKey:@"Items"] objectForKey:@"Item"]];
                        }
                    
                }
            }
            [self.table reloadData];
            loadMore.frame=CGRectMake(0, self.table.contentSize.height-35, self.table.frame.size.width, 35);
            if (total==movieShowArray.count) {
                loadMore.loadingTitle.text=@"没有更多";
            }
            else{
                loadMore.loadingTitle.text=@"上拉加载更多";
            }
            [loadMore.loadingAni stopAnimating];
        
        }
    }
    loadingmore=NO;
}

-(void)requestFailed:(ASIHTTPRequest*)request{
    [loadMore.loadingAni stopAnimating];
    loadMore.loadingTitle.text=@"无网络";
    loadingmore=NO;
}


@end
